#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
import json
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities['loggingPrefs'] = {'performance': 'ALL'}


class AutoSpider(object):
    def __init__(self):
        self.init_browder()
        self.company_info_dict = {}

    def init_browder(self):
        options = webdriver.FirefoxOptions()
        options.add_argument("disable-blink-features=AutomationControlled")  # 就是这一行告诉chrome去掉了webdriver痕迹
        self.browser = webdriver.Firefox(executable_path='./geckodriver.exe',
                                    options=options, desired_capabilities=desired_capabilities)
        self.wai = WebDriverWait(self.browser, 20)  # 等待20s后抛出异常
        self.start_year = 2022

    def login_index(self,username="13620209221",pwd='123456'):
        login_btn = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(@class,"loyo-data-terrace-layouts-header-login")]')))
        login_btn.click()

        username_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'phone')))
        pwd_elem = self.wai.until(EC.element_to_be_clickable((By.ID, "passwd")))
        submit_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//button[contains(@class,"ant-btn-primary")]')))
        # 使用账号密码登陆
        username_elem.send_keys(username)
        pwd_elem.send_keys(pwd)
        submit_elem.click()
        time.sleep(2)

    def login(self,username="13620209221",pwd='123456'):
        username_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'username')))
        pwd_elem = self.wai.until(EC.element_to_be_clickable((By.ID, "password")))
        # 使用账号密码登陆
        username_elem.send_keys(username)
        pwd_elem.send_keys(pwd)
        submit_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//button[contains(@class,"submit")]')))
        submit_elem.click()
        time.sleep(2)

    def close_alert(self):
        first_alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                 '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-xkb-active-modal-index-arrow-delete"]')))
        first_alter_elem.click()
        alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                           '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-favorite-guide-index-close"]')))
        alter_elem.click()
        button_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="driver-close-btn"]')))
        button_elem.click()
        time.sleep(1)

    def change_search_param(self,left_times=1,right_times=1):
        search_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"ant-btn-primary")]/span[contains(text(),"筛")]/..')))
        start_time_elem,end_time_elem = self.browser.find_elements_by_xpath('//input[contains(@class,"ant-calendar-picker-input")]')

        if left_times:
            start_time_elem.click()
            left_last_year_elem = self.wai.until(EC.element_to_be_clickable(
                (By.XPATH, '//a[contains(@class,"ant-calendar-prev-year-btn")]')))
            for i in range(left_times):
                left_last_year_elem.click()
            left_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                       '//td[contains(@class,"ant-calendar-selected-day")]')))
            left_day_elem.click()
            right_last_year_elem = self.wai.until(EC.element_to_be_clickable(
                (By.XPATH, '//a[contains(@class,"ant-calendar-prev-year-btn")]')))
            for i in range(right_times):
                right_last_year_elem.click()
            right_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                       '//td[contains(@class,"ant-calendar-selected-day")]')))
            right_day_elem.click()

        search_elem.click()
        time.sleep(2)

    def wait_company_info_lode(self):
        self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="ant-spin-nested-loading"]')))

        time.sleep(1)



    # def save_company_info(self):
    #     company_td_elems = self.browser.find_elements_by_xpath('//td[@class="ant-table-selection-column"]/following-sibling::td[1]')
    #     for elem in company_td_elems:
    #         time.sleep(1)
    #         try:
    #             elem.click()
    #             try:
    #                 contact_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"联系方式")]')))
    #                 contact_tab_elem.click()
    #                 mobile_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"手机")]')))
    #                 mobile_tab_elem.click()
    #                 get_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"ant-btn-primary")]/span[contains(text(),"领取线索")]/..')))
    #                 get_elem.click()
    #
    #                 ActionChains(self.browser).move_to_element(elem).perform()
    #             except:
    #                 print('no source')
    #             file_name = '精线索/' + str(time.time())
    #             with open(file_name, 'w', encoding='utf-8') as f:
    #                 f.write(self.browser.page_source)
    #                 self.parse_source_code(self.browser.page_source)
    #         except:
    #             print('error')

    def save_company_info(self):
        company_td_elems = self.browser.find_elements_by_xpath('//td[@class="ant-table-selection-column"]/following-sibling::td[1]')
        is_end = False
        for elem in company_td_elems:
            try:
                if not is_end:
                    self.browser.execute_script("arguments[0].scrollIntoView();", elem)
                    ActionChains(self.browser).move_to_element(elem).click().perform()
            except:
                print('翻到底了，跳过')
                is_end = True

            time.sleep(1)
            try:
                contact_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"联系方式")]')))
                contact_tab_elem.click()
                mobile_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"手机")]')))
                mobile_tab_elem.click()
                get_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@aria-hidden="false"]/div[@class="tableMask___14UFV"]//button[contains(@class,"ant-btn-primary")]/span[contains(text(),"领取线索")]/..')))
                get_elem.click()
                time.sleep(2)
                file_name = '精线索/' + str(time.time())
                with open(file_name, 'w', encoding='utf-8') as f:
                    f.write(self.browser.page_source)
                    self.parse_source_code(self.browser.page_source)
            except Exception as e:
                print(e)
            company_td_elems = self.browser.find_elements_by_xpath(
                '//td[@class="ant-table-selection-column"]/following-sibling::td[1]')

    def parse_source_code(self,text):
        from lxml import etree
        selector = etree.HTML(text,parser=etree.HTMLParser(encoding='utf-8'))
        company_name = ''.join(selector.xpath('//h1[@class="title___3uDQf"]/text()')).strip()
        tr_selectors = selector.xpath('//div[contains(@class,"ant-tabs-tabpane-active")]//div[contains(@class,"ant-tabs-tabpane-active")]//tr[contains(@class,"ant-table-row")]')
        if not self.company_info_dict.get(company_name,{}):
            self.company_info_dict[company_name] = {
                'phone_ls':[]
            }
        self.company_info_dict[company_name]['start_year'] = self.start_year
        phone_count = 0
        save_ls = []
        for div_selector in tr_selectors:
            phone = ''.join(div_selector.xpath('./td//h4/text()')).strip()
            source = ''.join(div_selector.xpath('./td[@class="ant-table-row-cell-break-word"]/a/text()'))
            source_2 = ''.join(div_selector.xpath('string(./td[@class="ant-table-row-cell-break-word"]/div[contains(@class,"ant-dropdown-trigger")])'))
            source_ls = div_selector.xpath('./td[@class="ant-table-row-cell-break-word"]/text()')
            source_ls.append(source)
            source_ls.append(source_2)
            source_ls = [i for i in source_ls if i]
            source_href_ls = div_selector.xpath('./td[@class="ant-table-row-cell-break-word"]/a/@href')
            save_ls.append([source_ls, source_href_ls, phone])
            self.company_info_dict[company_name]['phone_ls'].append({
                "phone":phone,
                'source_ls':source_ls,
                'source_href_ls': source_href_ls
            })
            phone_count += 1
            print(company_name,phone,source_ls,source_href_ls)
            print('==============================')
        self.company_info_dict[company_name]['phone_count'] = phone_count
        for ls in save_ls:
            with open('jz_info_jxs.csv', 'a', encoding='utf-8') as f:
                start_year = self.company_info_dict[company_name].get('start_year')
                f.write(f'{company_name},{start_year},{phone_count},{ls[2]},{str(ls[0])},{str(ls[1])}\n')
    def main(self):
        self.browser.maximize_window()
        self.browser.get('https://sales.jingxiansuo.com/#/user/login')
        # 登录
        self.login()
        self.browser.get('https://sales.jingxiansuo.com/#/searchCustomer/advanced-search')
        # 使用已保存高晒条件
        save_elem = self.wai.until(
            EC.element_to_be_clickable((By.XPATH, '//div[@class="initBoxItem___wQtZi"]/span[contains(text(),"已保存")]')))
        save_elem.click()
        use_btn_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//button[contains(@class,"ant-btn-primary")]/span[contains(text(),"用")]/..')))
        use_btn_elem.click()

        for i in range(8):
            self.start_year -= i
            # 进入高级搜索

            # 更改搜索条件
            self.change_search_param(left_times=i,right_times=i)
            # time.sleep(1000)
            # 等待 公司信息加载
            self.wait_company_info_lode()
            self.save_company_info()
            time.sleep(3)
        with open('result_jxs.json','w',encoding='utf-8') as f:
            json.dump(self.company_info_dict,f,ensure_ascii=False,indent=4)


if __name__ == '__main__':
    spider = AutoSpider()
    spider.main()
